package com.sky.controller.user;

import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "订单控制器")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 取消订单
     *
     * @param
     * @return
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Result cancelOrder(OrdersCancelDTO ordersCancelDTO) {
        log.info("[取消订单]ordersCancelDTO:{}", ordersCancelDTO);
        orderService.cancelOrderById(ordersCancelDTO);
        return Result.success();
    }

    @ApiOperation("再来一单")
    @PostMapping("repetition/{id}")
    public Result againOrder(@PathVariable Long id) {
        orderService.againOrder(id);
        return Result.success();


    }

    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> getOrderDetailId(@PathVariable Long id) {

        log.info("[查询订单详情]Id：{}", id);
        OrderVO orderVO = orderService.getOrderDetailId(id);
        return Result.success(orderVO);

    }

    @ApiOperation("获取历史订单")
    @GetMapping("/historyOrders")
    public Result<PageResult> getHistoryOrders(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("获取历史订单");
        PageResult pageResult = orderService.getHistoryOrders(ordersPageQueryDTO);

        return Result.success(pageResult);
    }


    //用户下单

    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> sumbit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        //打印
        log.info("[用户下单]OrdersSubmitDTO：{}", ordersSubmitDTO);
        //调用
        OrderSubmitVO orderSubmitVO = orderService.submit(ordersSubmitDTO);
        //响应
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        // 直接更新订单的状态
        orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
        //OrderPaymentVO orderPaymentVO =  orderService.payment(ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = new OrderPaymentVO();
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    // 用户催单
    @GetMapping("/reminder/{id}")
    @ApiOperation("用户催单")
    public Result reminder(@PathVariable Long id) {
        //打印
        log.info("[用户催单]id：{}", id);
        //调用
        orderService.reminder(id);
        //响应
        return Result.success();
    }
}
